<template>
  <el-row align="middle" type="flex" style="width:100%; heiht:40px">
    <el-col>
      <i v-if="isRoot" class="el-icon-office-building" />
      <span>{{ treeNode.name }}</span>
    </el-col>
    <el-col :span="4">
      <el-row type="flex" justify="end">
        <el-col>{{ treeNode.manager }}</el-col>
        <el-col>
          <el-dropdown @command="operateDepts">
            <span class="el-dropdown-link">
              操作
              <i class="el-icon-arrow-down el-icon--right" />
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item command="addDepartment">添加子部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="editDepartment">编辑部门</el-dropdown-item>
              <el-dropdown-item v-if="!isRoot" command="delDepartment">删除部门</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartments } from '@/api/departments'
export default {
  // 组件名称
  name: 'TreeTools',
  // 局部注册的组件
  components: {},
  // 组件参数 接收来自父组件的数据
  props: {
    treeNode: {
      type: Object,
      required: true
    },
    isRoot: {
      type: Boolean,
      default: false // 默认传值
    }
  },
  // 组件状态值
  data() {
    return {}
  },
  // 计算属性
  computed: {},
  // 侦听器
  watch: {},
  // 组件方法
  methods: {
    async operateDepts(command) {
      if (command === 'addDepartment') {
        // 添加部门
        this.$emit('addDepts', this.treeNode) // 告诉父组件 显示弹层
      } else if (command === 'editDepartment') {
        // 编辑部门
        this.$emit('edit', this.treeNode) // 告诉父组件 显示弹层
      } else if (command === 'delDepartment') {
        // 删除部门
        await this.$confirm('确定要删除吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        })
        await delDepartments(this.treeNode.id)
        this.$emit('delDepts') // 通知父组件进行更新
        this.$message.success('删除部门成功')
      }
    }
  }
}
</script>

<style scoped>
.el-icon-office-building {
  font-size: 18px;
  margin-right: 4px;
  font-weight: 700;
}
</style>
